package absolute_zero.com.urbox;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import android.os.Environment;
import android.util.Log;


public class ContactFileCreater
{
	private String name;
	private ArrayList<String> numbers;
	private ArrayList<String> emails;
	private ArrayList<String> extras;
	private byte[] picture;
	private File root;
	
	public ContactFileCreater()
	{
		name = null;
		numbers = new ArrayList<String>();
		emails = new ArrayList<String>();
		extras = new ArrayList<String>();
		picture = null;
		root = Environment.getExternalStorageDirectory();

	}
	public ContactFileCreater(String newName, ArrayList<String> newNumbers, ArrayList<String> newEmails, ArrayList<String> newExtras, byte[] newPicture)
	{
		name = newName;
		numbers = newNumbers;
		emails = newEmails;
		extras = newExtras;
		picture = newPicture;
	}
	public void addNumber(String number) //type as in mobile, home, work, fax, other...
	{
		numbers.add(number);
	}
	public void setName(String name) //pretty obvious
	{
		this.name = name;
	}
	public void addEmail(String email) //type as in home, work, other...
	{
		emails.add(email);
	}
	public void addExtra(String extra)
	{
		extras.add(extra);
	}
	public void setPicture(byte[] data) //this can be translated into a bitmap on the other end
	{
		picture = data;
	}
	public boolean createFile() //will create file contact.ctc on the android file system (i hope, may need to add permission or something to android manifest)
	//returns false if write fails (because there is no set name most likely)
	{
		if (name == null)
			return false;
		else
		{
			try
			{
				Log.d("File Location:", Environment.getExternalStorageDirectory().toString() + "/urbox/tmp/contact.ctc");
				BufferedWriter output = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory().toString() + "/urbox/tmp/contact.ctc"));
				output.write(name);
				output.newLine();
				for (String row: numbers)
				{
					output.write(row);
					output.newLine();
				}
				for (String row: emails)
				{
					output.write(row);
					output.newLine();
				}
				for (String row: extras)
				{
					output.write(row);
					output.newLine();
				}
				output.write("Photo ");
				output.newLine();
				if (picture != null)
				{
					for (byte value: picture)
					{
						output.write(Byte.toString(value));
						output.newLine();
					}
				}
				else
					output.write("--No Photo--");
				output.close();
				return true;
			}
			catch (IOException e)
			{
				Log.e("Write Error:", e.getMessage());
				return false;
			}
		}
	}
}
